package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcast.aliyunossspringbootautoconfigure.AliOSSUtils;
import com.itheima.health.common.ResultPageData;
import com.itheima.health.entity.TCheckgroup;
import com.itheima.health.entity.TCheckgroupCheckitem;
import com.itheima.health.entity.TSetmeal;
import com.itheima.health.entity.TSetmealCheckgroup;
import com.itheima.health.mapper.TSetmealMapper;
import com.itheima.health.service.ITCheckgroupService;
import com.itheima.health.service.ITSetmealCheckgroupService;
import com.itheima.health.service.ITSetmealService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.utils.PageResult;
import com.itheima.health.utils.QueryPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cpanda
 * @since 2024-01-26
 */
@Service
public class TSetmealServiceImpl extends ServiceImpl<TSetmealMapper, TSetmeal> implements ITSetmealService {

    @Autowired
    private ITCheckgroupService checkgroupService;
    @Autowired
    private ITSetmealCheckgroupService setmealCheckgroupService;

    @Autowired
    private AliOSSUtils aliOSSUtils;

    /**
     * 套餐列表
     */
    @Override
    public ResultPageData findPage(QueryPageBean dto) {
        // 设置分页条件
        Page<TSetmeal> page = new Page<>(dto.getCurrentPage(), dto.getPageSize());
        LambdaQueryWrapper<TSetmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(dto.getQueryString() != null, TSetmeal::getHelpCode, dto.getQueryString());
        Page<TSetmeal> checkgroupPage = this.page(page, wrapper);
        List<TSetmeal> records = checkgroupPage.getRecords();
        ResultPageData pageVO = new ResultPageData();
        pageVO.setTotal(checkgroupPage.getTotal());
        pageVO.setRows(records);
        return pageVO;
    }

    /**
     * 检查组展示
     */
    @Override
    public List<TCheckgroup> checkgroupfindAll() {
        List<TCheckgroup> list = checkgroupService.list();
        return list;
    }

    /**
     * 图片上传
     */
    @Override
    public String upload(MultipartFile imgFile) throws IOException {
        return aliOSSUtils.upload(imgFile);
    }

    /**
     * 套餐新增
     */
    @Override
    public void addSetmeal(TSetmeal setmeal, List<Integer> checkgroupIds) {
        this.save(setmeal);
        for (Integer checkgroupId : checkgroupIds) {
            TSetmealCheckgroup tSetmealCheckgroup = new TSetmealCheckgroup();
            tSetmealCheckgroup.setCheckgroupId(checkgroupId);
            tSetmealCheckgroup.setSetmealId(setmeal.getId());
            setmealCheckgroupService.save(tSetmealCheckgroup);
        }
    }

    /**
     * 根据id回显套餐信息
     */
    @Override
    public TSetmeal setmealById(Integer id) {
        TSetmeal setmeal = this.getById(id);
        return setmeal;
    }

    /**
     * 根据id回显套餐关联检查组信息
     */
    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(Integer setmealId) {
        List<TSetmealCheckgroup> list = setmealCheckgroupService.list();
        List<Integer> checkitemList = list.stream().filter(c -> Objects.equals(c.getSetmealId(), setmealId)).map(c -> c.getCheckgroupId()).collect(Collectors.toList());
        return checkitemList;
    }

    /**
     * 更新套餐
     */
    @Override
    public void updateSetmeal(TSetmeal setmeal, List<Integer> checkgroupIds) {
        //构造条件
        LambdaQueryWrapper<TSetmealCheckgroup> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TSetmealCheckgroup::getSetmealId, setmeal.getId());
        //先删除原来所有 checkgroupId所对应的checkitemId
        setmealCheckgroupService.remove(queryWrapper);
        for (Integer checkgroupId : checkgroupIds) {
            TSetmealCheckgroup tSetmealCheckgroup = new TSetmealCheckgroup();
            tSetmealCheckgroup.setCheckgroupId(checkgroupId);
            tSetmealCheckgroup.setSetmealId(setmeal.getId());
            setmealCheckgroupService.save(tSetmealCheckgroup);
        }
        this.updateById(setmeal);
    }
    /**
     * 根据id删除套餐
     */
    @Override
    public void deleteSetmeal(Integer id) {
        //构造条件
        LambdaQueryWrapper<TSetmealCheckgroup> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TSetmealCheckgroup::getSetmealId, id);
        //先删除原来所有 checkgroupId所对应的checkitemId
        setmealCheckgroupService.remove(queryWrapper);
        this.removeById(id);
    }
}
